package com.wtb.javatool.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wtb.javatool.common.PageResult;
import com.wtb.javatool.dao.StuApplyMapper;
import com.wtb.javatool.dao.TciMapper;
import com.wtb.javatool.entity.Tci;
import com.wtb.javatool.vo.StuApplyDetailDto;
import com.wtb.javatool.vo.StuApplyDto;
import com.wtb.javatool.vo.StuApplyRecordPageDto;
import com.wtb.javatool.vo.StuAuthDto;
import com.wtb.javatool.entity.StuApply;
import com.wtb.javatool.service.IStuApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StuApplyService implements IStuApplyService {

    @Autowired
    private StuApplyMapper stuApplyMapper;
    @Autowired
    private TciMapper tciMapper;

    public PageResult stuApplypageQuery1(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        String status = "1";
        Page<StuApply> page = stuApplyMapper.pageQuery(name,status);
        long total = page.getTotal();
        List<StuApply> result = page.getResult();
        return new PageResult(total, result);
    }

    public PageResult stuApplyRecordpageQuery( StuApplyRecordPageDto stuApplyRecordPageDTO) {
        PageHelper.startPage(stuApplyRecordPageDTO.getPageNum(), stuApplyRecordPageDTO.getPageSize());
        Page<StuApply> page = stuApplyMapper.stuApplyRecordpageQuery(stuApplyRecordPageDTO);
        long total = page.getTotal();
        List<StuApply> result = page.getResult();
        return new PageResult(total, result);
    }

    public void doAuthorize(StuAuthDto stuAuthDTO) {
        StuApply stuApply = new StuApply();
        BeanUtils.copyProperties(stuAuthDTO, stuApply);
        stuApply.setTeacher_auth_time(LocalDateTime.now());
        stuApply.setStatus("4");
        stuApplyMapper.update(stuApply);
    }

    public PageResult stuApplypageQuery2(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        String status = "4";
        Page<StuApply> page = stuApplyMapper.pageQuery(name, status);
        long total = page.getTotal();
        List<StuApply> result = page.getResult();
        return new PageResult(total, result);
    }

    public void newApply(StuApplyDto stuApplyDTO) {
        StuApply stuApply = new StuApply();
        BeanUtils.copyProperties(stuApplyDTO, stuApply);
        stuApply.setApply_time(LocalDateTime.now());
        stuApply.setStatus("1");
        stuApply.setApproverId("1");
        stuApplyMapper.insert(stuApply);
    }

    public void adminDoAuthorize(int id) {
        // 查询该申请所有数据
        StuApply stuApplyData = stuApplyMapper.selectById(id);
        StuApply stuApply = new StuApply();
        stuApply.setId(id);
        stuApply.setTeacher_auth_time(LocalDateTime.now());
        stuApply.setStatus("3");
        stuApply.setAdmin_auth_time(LocalDateTime.now());
        //根据courseId(JXBMC)查询tci表获取课程号(PK_ID)和学期号(KCDM)
        Tci tci = tciMapper.selectByJxbmc1(
               stuApplyData.getCourse_name()
        );
        if (tci == null) {
            System.out.println("end_tci");
        }
        String kechenghao = tci.getPK_ID();
        String xueqihao = tci.getXQ();
        //获得databaseName
        String databaseName = generateDatabaseName(stuApplyData.getCourse_name(), kechenghao, xueqihao, stuApplyData.getStu_sno());
        //保存到申请表对应对象中
        stuApply.setDb_name(databaseName);
        stuApplyMapper.update(stuApply);
    }

    private String generateDatabaseName(String courseId, String approverId,
                                        String xueqihao, String studentId) {
        // 将中文转换为拼音，这里简化为直接使用原字符串，实际应用中应使用拼音转换工具
        String courseIdPinyin = convertToPinyin(courseId);
        String approverIdPinyin = convertToPinyin(approverId);

        return String.format("%s_%s_%s_stu_%s",
                courseIdPinyin, approverIdPinyin, xueqihao, studentId);
    }
    private String convertToPinyin(String chinese) {
        // 实际应用中应使用拼音转换工具如pinyin4j
        // 这里简化为去除空格和特殊字符
        return chinese.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
    }
    public void adminReject(int id) {
        StuApply stuApply = new StuApply();
        stuApply.setId(id);
        stuApply.setStatus("2");
        stuApply.setReject_reason("管理员拒绝");
        stuApply.setAdmin_auth_time(LocalDateTime.now());
        stuApplyMapper.update(stuApply);
    }

    public List<StuApplyDetailDto> stuListAll() {
        return stuApplyMapper.stuListAll();
    }

    public void teacherReject(int id, String rejectReason) {
        StuApply stuApply = new StuApply();
        stuApply.setId(id);
        stuApply.setTeacher_auth_time(LocalDateTime.now());
        stuApply.setAdmin_auth_time(LocalDateTime.now());
        stuApply.setStatus("2");
        stuApply.setReject_reason(rejectReason);
        stuApplyMapper.update(stuApply);
    }

    public void teacherDoAuthorize(int id) {
        StuApply stuApply = new StuApply();
        stuApply.setId(id);
        stuApply.setTeacher_auth_time(LocalDateTime.now());
        stuApply.setStatus("4");
        stuApplyMapper.update(stuApply);
    }

    public void techerStunewApply(StuApplyDto stuApplyDTO) {
        StuApply stuApplyData = stuApplyMapper.selectByStuSno(stuApplyDTO.getStu_sno());
        StuApply stuApply = new StuApply();
        BeanUtils.copyProperties(stuApplyDTO, stuApply);
        stuApply.setStu_name(stuApplyData.getStu_name());
        stuApply.setApply_time(LocalDateTime.now());
        stuApply.setStatus("4");
        stuApply.setApproverId("1");
        stuApplyMapper.insert(stuApply);
    }


}
